package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"short-video-dev/mysql"

	"short-video-dev/videorpc/internal/svc"
	"short-video-dev/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoStatusLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoStatusLogic {
	return &VideoStatusLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 切换视频状态
func (l *VideoStatusLogic) VideoStatus(in *videorpc.VideoStatusRequest) (*videorpc.VideoStatusResponse, error) {
	// todo: add your logic here and delete this line

	var video mysql.VideoInfo
	err := video.VideoFindById(mysql.DB, in.VideoId, in.UserId)
	if err != nil {
		return nil, errors.New("视频不存在")
	}

	if in.TypeId == 1 {
		video = mysql.VideoInfo{
			Model:       gorm.Model{ID: uint(in.VideoId)},
			VideoStatus: int(in.VideoStatus),
		}

		err = video.VideoStatusUp(mysql.DB)
		if err != nil {
			return nil, errors.New("修改失败")
		}
	} else if in.TypeId == 0 {
		err := video.VideoDel(mysql.DB, in.VideoId)
		if err != nil {
			return nil, errors.New("删除失败")
		}
	}
	return &videorpc.VideoStatusResponse{}, nil
}
